package climspace.ejb;

import java.io.StringWriter;
import java.util.Date;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class KmlOutputFormat implements OutputFormatInterface {

	public String id;
	public String name;
	public String address;
	public String lat;
	public String lng;
	public String type;
	public double temperature;
	public double gust;
	private double altimeter;
	private double dewPoint;
	private Date entryDateTime;
	private double mNetId;
	private double precipitation;
	private double relativeHumidity;
	private double seaLevelPressure;
	private double stationElevation;
	private double stationLatitude;
	private double stationLongitude;
	private double windDirection;
	private double windSpeed;
	private double weatherCode;

	@Override
	public String objectToText(WeatherDataOutput weatherDataOutput) {

		KmlOutputFormat KML = new KmlOutputFormat();
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			TransformerFactory tranFactory = TransformerFactory.newInstance();
			Transformer aTransformer = tranFactory.newTransformer();
			Document doc = builder.newDocument();
			Element root = doc.createElement("kml");
			root.setAttribute("xmlns", "http://earth.google.com/kml/2.1");
			doc.appendChild(root);
			Element dnode = doc.createElement("Document");
			root.appendChild(dnode);
			Element rstyle = doc.createElement("Style");
			rstyle.setAttribute("id", "restaurantStyle");
			Element ristyle = doc.createElement("IconStyle");
			ristyle.setAttribute("id", "restaurantIcon");
			Element ricon = doc.createElement("Icon");
			Element riconhref = doc.createElement("href");
			riconhref
					.appendChild(doc
							.createTextNode("http://maps.google.com/mapfiles/kml/pal2/icon63.png"));
			rstyle.appendChild(ristyle);
			ricon.appendChild(riconhref);
			ristyle.appendChild(ricon);
			dnode.appendChild(rstyle);
			Element bstyle = doc.createElement("Style");
			bstyle.setAttribute("id", "barStyle");
			Element bistyle = doc.createElement("IconStyle");
			bistyle.setAttribute("id", "barIcon");
			Element bicon = doc.createElement("Icon");
			Element biconhref = doc.createElement("href");
			biconhref
					.appendChild(doc
							.createTextNode("http://maps.google.com/mapfiles/kml/pal2/icon27.png"));
			bstyle.appendChild(bistyle);
			bicon.appendChild(biconhref);
			bistyle.appendChild(bicon);
			dnode.appendChild(bstyle);
			
			KML.id = weatherDataOutput.getPrimary_id();
			KML.name = weatherDataOutput.getStation_name();
			KML.temperature = weatherDataOutput.getTemperature();
			KML.gust = weatherDataOutput.getWind_gust();
			KML.altimeter = weatherDataOutput.getAltimeter();
			KML.dewPoint = weatherDataOutput.getDew_point();
			KML.entryDateTime = weatherDataOutput.getEntry_datetime();
			KML.mNetId = weatherDataOutput.getMnet_id();
			KML.precipitation = weatherDataOutput.getPrecipitation();
			KML.relativeHumidity = weatherDataOutput.getRelative_humidity();
			KML.seaLevelPressure = weatherDataOutput.getSea_level_pressure();
			KML.stationElevation = weatherDataOutput.getStation_elevation();
			KML.stationLatitude = weatherDataOutput.getStation_latitude();
			KML.stationLongitude = weatherDataOutput.getStation_longitude();
			KML.windDirection = weatherDataOutput.getWind_direction();
			KML.windSpeed = weatherDataOutput.getWind_speed();
			KML.weatherCode = weatherDataOutput.getWeather_code();

			Element placemark = doc.createElement("Placemark");
			dnode.appendChild(placemark);
			Element name = doc.createElement("nam");
			name.appendChild(doc.createTextNode(KML.name));

			placemark.appendChild(name);
			Element descrip = doc.createElement("description");
			descrip.appendChild(doc.createTextNode("Station Name :"
					+ (KML.name) + "\n"));
			descrip.appendChild(doc.createTextNode("Temperature :"
					+ (KML.temperature) + " Faren \n"));
			descrip.appendChild(doc
					.createTextNode("\nGust Value : " + (KML.gust)));
			descrip.appendChild(doc
					.createTextNode("\naltimeter: " + (KML.altimeter)));
			descrip.appendChild(doc
					.createTextNode("\ndewPoint: " + (KML.dewPoint)));
			descrip.appendChild(doc
					.createTextNode("\nentryDateTime: " + (KML.entryDateTime)));
			descrip.appendChild(doc
					.createTextNode("\nmNetId: " + (KML.mNetId)));
			descrip.appendChild(doc
					.createTextNode("\nprecipitation: " + (KML.precipitation)));
			descrip.appendChild(doc
					.createTextNode("\nrelativeHumidity: " + (KML.relativeHumidity)));
			descrip.appendChild(doc
					.createTextNode("\nseaLevelPressure: " + (KML.seaLevelPressure)));
			descrip.appendChild(doc
					.createTextNode("\nstationElevation: " + (KML.stationElevation)));
			descrip.appendChild(doc
					.createTextNode("\nstationLatitude: " + (KML.stationLatitude)));
			descrip.appendChild(doc
					.createTextNode("\nstationLongitude: " + (KML.stationLongitude)));
			descrip.appendChild(doc
					.createTextNode("\nwindDirection: " + (KML.windDirection)));
			descrip.appendChild(doc
					.createTextNode("\nwindSpeed: " + (KML.windSpeed)));
			descrip.appendChild(doc
					.createTextNode("\nweatherCode: " + (KML.weatherCode)));
			placemark.appendChild(descrip);
			Element styleUrl = doc.createElement("styleUrl");
			styleUrl.appendChild(doc.createTextNode("#" + KML.type + "Style"));
			placemark.appendChild(styleUrl);
			Element point = doc.createElement("Point");
			Element coordinates = doc.createElement("coordinates");
			coordinates.appendChild(doc.createTextNode(KML.stationLongitude
					+ "," + KML.stationLatitude));
			point.appendChild(coordinates);
			placemark.appendChild(point);
			Source src = new DOMSource(doc);
			
			StringWriter kmlWriter = new StringWriter();
			Result dest = new StreamResult(kmlWriter);
			
			aTransformer.transform(src, dest);
			System.out.println("KML generation completed.....");
			return kmlWriter.toString();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return "";
	}

}
